package models.dao;

import java.util.ArrayList;
import java.util.List;

import models.entities.ModuleStaff;
import models.enumtype.KPaging;
import models.enumtype.StaffRoleType;
import models.repo.IRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import scala.annotation.meta.param;

@Service
public class ModuleStaffDAO {
	@Autowired
	private IRepository repo;
	
	public ModuleStaff createModule(ModuleStaff moduleStaff) throws Exception {
		if (moduleStaff == null) {
			throw new IllegalArgumentException("module");
		}

		repo.create(moduleStaff);
		
		return repo.getEntityById(ModuleStaff.class, moduleStaff.getId());
	}

	public void deleteModule(ModuleStaff moduleStaff) throws Exception {
		if (moduleStaff == null) {
			throw new IllegalArgumentException("module");
		}
		repo.delete(moduleStaff);
		
	}

	public ModuleStaff getModuleById(Long id) throws Exception {
		return repo.getEntityById(ModuleStaff.class, id);
	}
	
	
	public void updateShop(ModuleStaff moduleStaff) throws Exception {
		if (moduleStaff == null) {
			throw new IllegalArgumentException("shop");
		}
		
		repo.update(moduleStaff);
	}
	
	public ModuleStaff getModuleStaffByModuleIdStaffId(Long moduleId, Long staffId) throws Exception {
		StringBuilder sql = new StringBuilder();
		List<Object> params = new ArrayList<Object>();
		sql.append("select * from MODULE_STAFF where 1 = 1 ");
		if(moduleId != null) {
			sql.append(" and MODULE_ID = ? ");
			params.add(moduleId);
		}
		if(staffId != null) {
			sql.append(" and STAFF_ID = ? ");
			params.add(staffId);
		}
		return repo.getEntityBySQL(ModuleStaff.class, sql.toString(), params);
	}
	
	public List<ModuleStaff> getListModuleStaff(KPaging<ModuleStaff> kPaging, Long moduleId) throws Exception {
		StringBuilder sql = new StringBuilder();
		List<Object> params = new ArrayList<Object>();
		sql.append("select * from MODULE_STAFF where 1 = 1");
		if(moduleId != null) {
			sql.append(" and MODULE_ID = ? ");
			params.add(moduleId);
		}
		if(kPaging != null) {
			return repo.getListBySQLPaginated(ModuleStaff.class, sql.toString(), params, kPaging);
		} else {
			return repo.getListBySQL(ModuleStaff.class, sql.toString(), params);
		}
	}
}
